package virtPresenter.model
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	
	import virtPresenter.event.SpeedareaScrollEvent;

	/**
	 * This class represents one Button, that is part of the Speedareas
	 * 
	 * @author: Sebastian Pospiech
	 */
	public class SpeedareaButton
	{
		public static var LEFT_ANIM:String = "left_anim";
		public static var LEFT_SLIDES:String = "left_slides";
		public static var RIGHT_ANIM:String = "right_anim";
		public static var RIGHT_SLIDES:String = "right_slides";
		
		private var button:MovieClip = new MovieClip();
		private var imageLoaderOut:Loader = new Loader();
		private var imageLoaderIn:Loader = new Loader();
		
		private var name:String;
		private var type:String;
		private var pathIn:String;
		private var pathOut:String;
		
		private var isActive:Boolean = false;
		
		public function SpeedareaButton(name:String, y:int, type:String){
			this.name = name;
			buildPath();
			this.type = type;
			button.y = y;
			button.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			button.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			button.addEventListener(MouseEvent.CLICK, onMouseClick);
			
			imageLoaderOut.contentLoaderInfo.addEventListener(Event.COMPLETE, loadOutCompleteHandler);
			imageLoaderOut.load(new URLRequest(pathOut));
			
			imageLoaderIn.contentLoaderInfo.addEventListener(Event.COMPLETE, loadInCompleteHandler);
			imageLoaderIn.load(new URLRequest(pathIn));
		}
			
		private function buildPath():void{
			pathIn = "gfx/"+this.name+"_in.gif";
			pathOut = "gfx/"+this.name+"_out.gif"
		}
		
		private function loadOutCompleteHandler(e:Event):void{
			button.addChild(imageLoaderOut);
			button.height = AS_Constants.SCRUBBER_HEIGHT+54;
			button.width = AS_Constants.WIDTH_PREVIEW_SIDES* 1/3;
		}
		
		private function loadInCompleteHandler(e:Event):void{
			button.addChild(imageLoaderIn);
			imageLoaderIn.visible = false;
		}
		
		private function onMouseOver(e:MouseEvent):void{
			this.setActivation(true);
		}
		
		private function onMouseOut(e:MouseEvent):void{
			this.setActivation(false);
		}
		
		public function setActivation(isActive:Boolean):void{
			this.isActive = isActive;
			if (this.isActive){
				imageLoaderOut.visible = false;
				imageLoaderIn.visible = true;
			}
			else{
				imageLoaderIn.visible = false;
				imageLoaderOut.visible = true;
			}
		}
		
		private function onMouseClick(e:MouseEvent):void{
			if (type.match(LEFT_ANIM)){
				CairngormEventDispatcher.getInstance().dispatchEvent(
					new SpeedareaScrollEvent(true, SpeedareaScrollEvent.ANIMATIONS));
			} else if (type.match(LEFT_SLIDES)){
				CairngormEventDispatcher.getInstance().dispatchEvent(
					new SpeedareaScrollEvent(true, SpeedareaScrollEvent.SLIDES));
			} else if (type.match(RIGHT_ANIM)){
				CairngormEventDispatcher.getInstance().dispatchEvent(
					new SpeedareaScrollEvent(false, SpeedareaScrollEvent.ANIMATIONS));
			} else if (type.match(RIGHT_SLIDES)){
				CairngormEventDispatcher.getInstance().dispatchEvent(
					new SpeedareaScrollEvent(false, SpeedareaScrollEvent.SLIDES));
			}
		}
		
		public function active():Boolean{
			return this.isActive;
		}
		
		public function addParent(s:Sprite):void{
			s.addChild(this.button);
		}
		
		public function getX():int{
			return button.x;
		}
		
		public function getWidth():int{
			return button.width;
		}
		
		public function getY():int{
			return button.y;
		}
		
		public function getHeight():int{
			return button.height;
		}
		
		public function setX(x:int):void{
			button.x = x;
		}
		
		public function isVisible(visible:Boolean):void{
			button.visible = visible;
		}
	}
}